﻿#include "Common/config.h"
#include "Http/WebSocketSession.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Util/File.h"
#include "Util/NoticeCenter.h"
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "cjsonconfigz.h"
#include "cserverManz.h"
#include "json/json.h"
#include <functional>
#include <iostream>
#include <map>
#include <signal.h>
#include <sys/stat.h>

using namespace std;
using namespace toolkit;
using namespace mediakit;

static onceToken token([]() {
    //
    string sTLogPath;
    string sTConfig;
    string stexename;
    stexename = exeName();

    sTLogPath = str_format("/tmp/logFile/%s/", stexename.data());
    sTConfig = str_format("%s/configFile/%s.json", exeDir().data(), stexename.data());

    // 配置文件路径不存在就自动创建,方便生成默认配置
    File::create_path(sTConfig.data(), S_IRWXO | S_IRWXG | S_IRWXU);
    //
    cjsonconfigz::Instance().SetdefultCFG("log.loglevel", LDebug);
    cjsonconfigz::Instance().SetdefultCFG("log.logfilepath", sTLogPath);
    cjsonconfigz::Instance().SetdefultCFG("log.logday", 5);

    cjsonconfigz::Instance().SetdefultCFG("mediaser.ip", "http://127.0.0.1:8093");
    cjsonconfigz::Instance().SetdefultCFG("mediaser.secret", "N21Ji5yZrqMbvHPSoHZH00zVAMM9uhvX");
    cjsonconfigz::Instance().SetdefultCFG("local.ip", "127.0.0.1");

    cjsonconfigz::Instance().SetdefultCFG("http.port", 8096);
    // 是否测试
    cjsonconfigz::Instance().SetdefultCFG("test.istest", 0);
    cjsonconfigz::Instance().SetdefultCFG("test.url", "rtsp://admin:pdf123456@192.168.0.64:554/Streaming/channels/501");
    cjsonconfigz::Instance().InitCfgFile(sTConfig);
});

int main(int argc, char *argv[]) {

    LogLevel logLevel = (LogLevel)cjsonconfigz::Instance().getIntbyKey("log.loglevel");
    string logfilepath = cjsonconfigz::Instance().getStringbyKey("log.logfilepath");
    int ilogday = cjsonconfigz::Instance().getIntbyKey("log.logday");

    logLevel = MIN(MAX(logLevel, LTrace), LError);
    ilogday = MIN(MAX(ilogday, 1), 30);

    //  设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", logLevel));

    auto fileChannel = std::make_shared<FileChannel>("FileChannel", logfilepath, logLevel);
    // 日志最多保存天数
    fileChannel->setMaxDay(ilogday);
    Logger::Instance().add(fileChannel);

    // 启动异步日志线程
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());

    InfoL << "程序启动 Start ...";

    //
    int ihttpport = cjsonconfigz::Instance().getIntbyKey("http.port");
    // 开启http服务器
    TcpServer::Ptr httpSrv(new TcpServer());
    httpSrv->start<HttpSession>(ihttpport);

    auto serverz = std::make_shared<cserverManz>();

    if (true == serverz->OnInit()) {
        serverz->OnStart();
    } else {
        return 0;
    }

    static semaphore sem;
    signal(SIGINT, [](int) {
        InfoL << "SIGINT:exit";
        signal(SIGINT, SIG_IGN);
        sem.post();
    });

    sem.wait();

    InfoL << "The program is exiting, please wait...";

    sleep(1);
    InfoL << "exit ok!";
    return 0;
}
